package com.qianbei.share.ui;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;

import android.support.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;

import com.qianbei.R;

import com.qianbei.common.base.BaseFragmentDialog;

import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.user.withdrawal.BindingAlipayActivity;


import java.text.SimpleDateFormat;
import java.util.Date;

import static com.qianbei.common.net.control.Constant.ISBINDING_ALIPAY;
import static com.qianbei.common.net.control.Constant.RED_WARS;

public class RobMoneyFragmentDialog extends BaseFragmentDialog {


    private TextView mTvRobContent;
    private ImageView mIvRobCopy;
    private String content;

    public static RobMoneyFragmentDialog newInstance(String content) {
        RobMoneyFragmentDialog f = new RobMoneyFragmentDialog();
        Bundle args = new Bundle();
        args.putString("content", content);
        f.setArguments(args);
        return f;
    }

    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getArguments() != null) {
            content = getArguments().getString("content");// ("typ");.
        }
        View v = View.inflate(getActivity(), R.layout.kh_rob_money, null);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        LinearLayout mLlRobCancel = (LinearLayout) v.findViewById(R.id.ll_rob_cancel);

        mTvRobContent = (TextView) v.findViewById(R.id.tv_rob_content);
        mIvRobCopy = (ImageView) v.findViewById(R.id.iv_rob_copy);

        mTvRobContent.setText(content);//设置红包内容(吱口令)

        mLlRobCancel.setOnClickListener(this);
        mIvRobCopy.setOnClickListener(this);
        mTvRobContent.setOnClickListener(this);
        return v;
    }


    public ShareViewClick click;

    public interface ShareViewClick {
        void shareClick(int viewId);
    }

    @Override
    public void onClick(View v) {
        if (click != null) {
            click.shareClick(v.getId());
        }
        dismiss();
        switch (v.getId()) {
            case R.id.iv_rob_copy:


                final SharedPreferences aliDate = mContext.getSharedPreferences("aliDate", 0);

                String aliDates = aliDate.getString("aliDates", "");


                SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
                Date curDate = new Date(System.currentTimeMillis());
                final String str = formatter.format(curDate);

                if (aliDates != null && !aliDates.equals(str)) {
                    ServerControl sr = new ServerControl(Request.Method.GET, ISBINDING_ALIPAY
                            + SetData.getUserID() + "?");
                    sr.serverListener = new ServerControl.ServerListener() {
                        @Override
                        public void serverFinish(ServerResult sr) {

                            if (sr.isContinue) {
                                String state = sr.bodyData.optJSONObject("data").optString("state");
                                if (state.equals("0")) {
                                    aliDate.edit().putString("aliDates", str).commit();

                                    Intent intent = new Intent(mContext, BindingAlipayActivity.class);
                                    intent.putExtra("task", "");
                                    mContext.startActivity(intent);
                                } else {
                                    openAlipay();
                                }
                            }
                        }
                    };
                    sr.startVolley();
                } else {
                    openAlipay();
                }


                break;
        }
    }

    private void openAlipay() {
        ClipboardManager clipboardManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
        //创建ClipData对象
        ClipData clipData = ClipData.newPlainText("rob", mTvRobContent.getText().toString());
        //添加ClipData对象到剪切板中
        clipboardManager.setPrimaryClip(clipData);

        try {
            Toast.makeText(mContext, "正在打开支付宝", Toast.LENGTH_SHORT).show();

            PackageManager packageManager
                    = mContext.getApplicationContext().getPackageManager();
            Intent intent = packageManager.
                    getLaunchIntentForPackage("com.eg.android.AlipayGphone");
            mContext.startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(mContext, "未检测到支付宝客户端，正在打开下载页", Toast.LENGTH_SHORT).show();

            String url = "https://ds.alipay.com/?from=mobileweb";
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            mContext.startActivity(intent);
        }
    }

}
